how to categorize data based on several conditions